package com.dgut.app.controller;

import com.dgut.app.service.DiaryService;
import com.dgut.app.service.FileService;
import com.dgut.common.dto.DiaryDTO;
import com.dgut.common.dto.PageDTO;
import com.dgut.common.utils.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author IT00ZYQ
 * @Date 2021/4/5 13:20
 **/
@RequestMapping("/app/diary")
@RestController
public class DiaryController {

    private final DiaryService diaryService;
    private final FileService fileService;

    public DiaryController(DiaryService diaryService, FileService fileService) {
        this.diaryService = diaryService;
        this.fileService = fileService;
    }

    /**
     * 添加日记接口
     * @param dto 日记信息
     * @param token 用户登录令牌
     * @return R
     */
    @PostMapping
    public R addDiary(@RequestBody DiaryDTO dto, @RequestHeader("token") String token) {
        diaryService.addDiary(dto, token);
        return R.ok();
    }

    /**
     * 日记附件上传接口
     * @param file 文件
     * @return R
     */
    @PostMapping("/attachment")
    public R uploadAttachment(MultipartFile file) {
        return R.ok().data("file", fileService.uploadFile(file));
    }

    /**
     * 分页获取日记列表
     * @param dto
     * @param token
     * @return
     */
    @GetMapping
    public R listDiary(PageDTO dto,
                       @RequestParam Date begin,
                       @RequestParam Date end,
                       @RequestHeader("token") String token) {
        return R.ok().data("page", diaryService.listDiary(token, dto,begin,end));
    }

    /**
     * 日记详情
     * @param diaryId
     * @return
     */
    @GetMapping("/detail")
    public R listDiary(Integer diaryId) {
        return R.ok().data("detail", diaryService.detail(diaryId));
    }

    /**
     * 删除日记
     * @param id 日记ID
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable(name = "id") Integer id) {
        diaryService.deleteById(id);
        return R.ok();
    }

    /**
     * 修改日记信息
     * @param dto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DiaryDTO dto) {
        diaryService.updateDiary(dto);
        return R.ok();
    }



}
